require('dotenv').config();

const app = require('fastify')({
	logger: {
		level: 'trace',
		prettyPrint: true
	}
}),
	fs   = require('fs'),
	path = require('path')

app.register(require('fastify-secure-session'), {
	cookieName: 'sexsion',
	key: fs.readFileSync(path.join(__dirname, 'kus-key')),
	cookie: {
		path: '/'
	}
})
	.register(require('fastify-helmet'), {
		enableCSPNonces: true,
		contentSecurityPolicy: {
			directives: {
				'default-src': ["'self'"],
				'base-uri': ["'self'"],
				'style-src': ["'self'", "'nonce-poes'"],
				'script-src': ["'self'", "nonce-js"],
				'img-src': ["'self'", 'i.ibb.co']
			}
		}
	})
	.register(require('fastify-formbody'))
	.register(require('fastify-flash'))
	.register(require('fastify-static'), {
		root: path.join(__dirname, '/assets/')
	})
	.register(require('point-of-view'), {
		engine: {
			handlebars: require('handlebars')
		},
		options: {
			partials: {
				header: './html/include/header.hbs',
				footer: './html/include/footer.hbs'
			}
		}
	})

const hebs = require('./html/hbs/helphbs.js')

// ROUTER
const { routes } = require('./routes')
routes(app);

// DB
const db = require('./db/config.js')


const startServer = () => {
	try{
		app.listen(process.env.PORT)
		console.log('Server running on port: ', process.env.PORT)
	}catch(err){
		app.log.error(err)
		process.exit(1)
	}
}

startServer();